package context

import (
	"gitee.com/swsk33/server-demo/initialize"
	"gitee.com/swsk33/server-demo/injector"
	"github.com/gin-gonic/gin"
	"strconv"
)

// InitGinRouterAndStart 初始化Gin Router并启动
func InitGinRouterAndStart() {
	// 创建router对象
	router := gin.Default()
	// 从wire注入器获取各个API对象（db对象在init-db.go中）
	characterApi := injector.InitializeCharacterAPI(db)
	// 定义路由组
	characterGroup := router.Group("/api/character")
	{
		characterGroup.POST("/add", characterApi.Add)
		characterGroup.DELETE("/delete/:id", characterApi.DeleteChar)
		characterGroup.PATCH("/update", characterApi.Update)
		characterGroup.GET("/get/:id", characterApi.Get)
		characterGroup.GET("/get-all", characterApi.GetAll)
	}
	// 获取配置
	config := initialize.GetServerConfig()
	portString := strconv.Itoa(config.Port)
	// 启动
	_ = router.Run(":" + portString)
}